home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / comm2 / termsorc.lha / Extras / Source / term-source.lha / termXEM.c < prev    next >
C/C++ Source or Header  |  1995-09-26  |  24KB  |  1,322 lines

  1. /*
  2. **    termXEM.c
  3. **
  4. **    External emulation support routines
  5. **
  6. **    Copyright © 1990-1995 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     /* GetOptionMode(struct xpr_option *Option):
  13.      *
  14.      *    Turn text into a boolean value.
  15.      */
  16.  
  17. STATIC BYTE __regargs
  18. GetOptionMode(struct xem_option *Option)
  19. {
  20.     if(Option)
  21.     {
  22.         STATIC STRPTR TrueOptions[] =
  23.         {
  24.             "ON",
  25.             "TRUE",
  26.             "T",
  27.             "YES",
  28.             "Y",
  29.             NULL
  30.         };
  31.  
  32.         register WORD i;
  33.  
  34.         for(i = 0 ; TrueOptions[i] ; i++)
  35.         {
  36.             if(!Stricmp(Option -> xemo_value,TrueOptions[i]))
  37.                 return(TRUE);
  38.         }
  39.     }
  40.  
  41.     return(FALSE);
  42. }
  43.  
  44. STATIC LONG __saveds __asm
  45. xem_sflush(VOID)
  46. {
  47.     return((LONG)FlushSerialRead());
  48. }
  49.  
  50. STATIC LONG __saveds __asm
  51. xem_squery(VOID)
  52. {
  53.     if(WriteRequest)
  54.     {
  55.         ULONG    Waiting;
  56.         UWORD    Status;
  57.  
  58.         GetSerialInfo(&Waiting,&Status);
  59.  
  60.             /* Return error if carrier is lost. */
  61.  
  62.         if((Status & CIAF_COMCD) && Config -> SerialConfig -> CheckCarrier && !Config -> SerialConfig -> DirectConnection)
  63.         {
  64.             ObtainSemaphore(&OnlineSemaphore);
  65.  
  66.             if(Online)
  67.             {
  68.                 WasOnline    = Online;
  69.                 Online        = FALSE;
  70.             }
  71.  
  72.             ReleaseSemaphore(&OnlineSemaphore);
  73.         }
  74.         else
  75.             return((LONG)Waiting);
  76.     }
  77.  
  78.     return(-1);
  79. }
  80.  
  81. STATIC LONG __saveds __asm
  82. xem_sread(register __a0 STRPTR Buffer,register __d0 LONG Size,register __d1 ULONG Timeout)
  83. {
  84.         /* Valid size parameter? */
  85.  
  86.     if(Size > 0)
  87.     {
  88.         LONG Total = 0;
  89.  
  90.             /* Return error if carrier is lost. */
  91.  
  92.         if(Config -> SerialConfig -> CheckCarrier && !Config -> SerialConfig -> DirectConnection)
  93.         {
  94.             if(GetSerialStatus() & CIAF_COMCD)
  95.             {
  96.                 ObtainSemaphore(&OnlineSemaphore);
  97.  
  98.                 if(Online)
  99.                 {
  100.                     WasOnline    = Online;
  101.                     Online        = FALSE;
  102.                 }
  103.  
  104.                 ReleaseSemaphore(&OnlineSemaphore);
  105.  
  106.                 return(-1);
  107.             }
  108.         }
  109.  
  110.             // Did the read request terminate, is data available?
  111.  
  112.         if(CheckSerialRead())
  113.         {
  114.                 // Pick it up...
  115.  
  116.             if(WaitSerialRead())
  117.             {
  118.                 RestartSerial();
  119.  
  120.                 return(0);
  121.             }
  122.  
  123.                 // One byte was read
  124.  
  125.             *Buffer++ = ReadBuffer[0];
  126.             Size--;
  127.             BytesIn++;
  128.             Total++;
  129.  
  130.                 // Is there still something to be read?
  131.  
  132.             if(Size > 0)
  133.             {
  134.                 ULONG    Waiting;
  135.                 LONG    Result;
  136.  
  137.                     // Check how many bytes are still
  138.                     // waiting to be read
  139.  
  140.                 if(Waiting = GetSerialWaiting())
  141.                 {
  142.                         // Don't read too many
  143.  
  144.                     if(Waiting > Size)
  145.                         Waiting = Size;
  146.  
  147.                     if(DoSerialRead(Buffer,Waiting))
  148.                         Result = 0;
  149.                     else
  150.                         Result = Waiting;
  151.                 }
  152.                 else
  153.                     Result = 0;
  154.  
  155.                 if(Result > 0)
  156.                 {
  157.                     Buffer    += Result;
  158.                     Size    -= Result;
  159.                     BytesIn    += Result;
  160.                     Total    += Result;
  161.                 }
  162.  
  163.                 Result++;
  164.  
  165.                 RestartSerial();
  166.  
  167.                     // Now check if there is a timeout value
  168.                     // given. If there is, we need to fill the
  169.                     // buffer with as many bytes as there are
  170.                     // requested and may not be able to return
  171.                     // right now. We will return if enough
  172.                     // data was read.
  173.  
  174.                 if(!Timeout || !Size)
  175.                     return(Result);
  176.             }
  177.             else
  178.             {
  179.                 RestartSerial();
  180.  
  181.                 return(1);
  182.             }
  183.         }
  184.         else
  185.         {
  186.                 // No data is available and no timeout
  187.                 // is given. We can't deliver anything,
  188.                 // so let's scram.
  189.  
  190.             if(!Timeout)
  191.                 return(0);
  192.         }
  193.  
  194.             // At this point we still need to read some data
  195.             // and a timeout is given.
  196.  
  197.         /* ALWAYS */
  198.         {
  199.             register ULONG    SerialMask = PORTMASK(ReadPort),
  200.                     SignalSet;
  201.  
  202.                 /* Set up the timer. */
  203.  
  204.             TimeRequest -> tr_node . io_Command    = TR_ADDREQUEST;
  205.             TimeRequest -> tr_time . tv_secs    = Timeout / MILLION;
  206.             TimeRequest -> tr_time . tv_micro    = Timeout % MILLION;
  207.  
  208.                 /* Prevent early termination. */
  209.  
  210.             ClrSignal(SIG_TIMER);
  211.  
  212.                 /* Start IO... */
  213.  
  214.             SendIO(TimeRequest);
  215.  
  216.             FOREVER
  217.             {
  218.                 SignalSet = Wait(SerialMask | SIG_TIMER);
  219.  
  220.                     /* Receive buffer filled? */
  221.  
  222.                 if(SignalSet & SerialMask)
  223.                 {
  224.                         /* Did the request terminate gracefully? */
  225.  
  226.                     if(WaitSerialRead())
  227.                         RestartSerial();
  228.                     else
  229.                     {
  230.                             // One byte was read
  231.  
  232.                         *Buffer++ = ReadBuffer[0];
  233.                         Size--;
  234.                         BytesIn++;
  235.                         Total++;
  236.  
  237.                             // Is there still something to be read?
  238.  
  239.                         if(Size > 0)
  240.                         {
  241.                             ULONG    Waiting;
  242.                             LONG    Result;
  243.  
  244.                                 // Check how many bytes are still
  245.                                 // waiting to be read
  246.  
  247.                             if(Waiting = GetSerialWaiting())
  248.                             {
  249.                                     // Don't read too many
  250.  
  251.                                 if(Waiting > Size)
  252.                                     Waiting = Size;
  253.  
  254.                                 if(DoSerialRead(Buffer,Waiting))
  255.                                     Result = 0;
  256.                                 else
  257.                                     Result = Waiting;
  258.                             }
  259.                             else
  260.                                 Result = 0;
  261.  
  262.                                 // Did we get an error?
  263.  
  264.                             if(Result > 0)
  265.                             {
  266.                                 Buffer    += Result;
  267.                                 Size    -= Result;
  268.                                 BytesIn    += Result;
  269.                                 Total    += Result;
  270.                             }
  271.                         }
  272.  
  273.                         RestartSerial();
  274.  
  275.                         if(!Size)
  276.                         {
  277.                                 /* Abort the timer request. */
  278.  
  279.                             if(!CheckIO(TimeRequest))
  280.                                 AbortIO(TimeRequest);
  281.  
  282.                             WaitIO(TimeRequest);
  283.  
  284.                             return(Total);
  285.                         }
  286.                     }
  287.                 }
  288.  
  289.                     /* Hit by timeout? */
  290.  
  291.                 if(SignalSet & SIG_TIMER)
  292.                 {
  293.                         /* Remove the timer request. */
  294.  
  295.                     WaitIO(TimeRequest);
  296.  
  297.                         /* Did the driver receive any
  298.                          * data?
  299.                          */
  300.  
  301.                     if(CheckSerialRead())
  302.                     {
  303.                             /* Did the request terminate gracefully? */
  304.  
  305.                         if(WaitSerialRead())
  306.                             RestartSerial();
  307.                         else
  308.                         {
  309.                                 // One byte was read
  310.  
  311.                             *Buffer++ = ReadBuffer[0];
  312.                             Size--;
  313.                             BytesIn++;
  314.                             Total++;
  315.  
  316.                                 // Is there still something to be read?
  317.  
  318.                             if(Size > 0)
  319.                             {
  320.                                 ULONG    Waiting;
  321.                                 LONG    Result;
  322.  
  323.                                     // Check how many bytes are still
  324.                                     // waiting to be read
  325.  
  326.                                 if(Waiting = GetSerialWaiting())
  327.                                 {
  328.                                         // Don't read too many
  329.  
  330.                                     if(Waiting > Size)
  331.                                         Waiting = Size;
  332.  
  333.                                     if(DoSerialRead(Buffer,Waiting))
  334.                                         Result = 0;
  335.                                     else
  336.                                         Result = Waiting;
  337.                                 }
  338.                                 else
  339.                                     Result = 0;
  340.  
  341.                                     // Did we get an error?
  342.  
  343.                                 if(Result > 0)
  344.                                 {
  345.                                     Size    -= Result;
  346.                                     BytesIn    += Result;
  347.                                     Total    += Result;
  348.                                 }
  349.                             }
  350.  
  351.                             RestartSerial();
  352.                         }
  353.                     }
  354.  
  355.                     return(Total);
  356.                 }
  357.             }
  358.         }
  359.     }
  360.     else
  361.         return(0);
  362. }
  363.  
  364. STATIC ULONG __saveds __asm
  365. xem_toptions(register __d0 LONG NumOpts,register __a0 struct xem_option **Opts)
  366. {
  367.     if(NumOpts && Opts)
  368.     {
  369.         enum    {    GAD_USE=1,GAD_CANCEL,GAD_SPECIAL };
  370.  
  371.         struct LayoutHandle    *Handle;
  372.         ULONG             Flags = NULL;
  373.  
  374.             /* We only have 32 bits! */
  375.  
  376.         if(NumOpts > 32)
  377.             NumOpts = 32;
  378.  
  379.         if(Handle = LT_CreateHandleTags(Window -> WScreen,
  380.             LH_LocaleHook,    &LocaleHook,
  381.         TAG_DONE))
  382.         {
  383.             struct Window    *PanelWindow;
  384.             LONG         i,Split;
  385.  
  386.             if(NumOpts > 16)
  387.                 Split = NumOpts / 2;
  388.             else
  389.                 Split = -1;
  390.  
  391.             LT_New(Handle,
  392.                 LA_Type,    VERTICAL_KIND,
  393.             TAG_DONE);
  394.             {
  395.                 LT_New(Handle,
  396.                     LA_Type,    HORIZONTAL_KIND,
  397.                     LA_LabelID,    MSG_V36_1501,
  398.                 TAG_DONE);
  399.                 {
  400.                     LT_New(Handle,
  401.                         LA_Type,    VERTICAL_KIND,
  402.                     TAG_DONE);
  403.                     {
  404.                         for(i = 0 ; i < NumOpts ; i++)
  405.                         {
  406.                             if(Opts[i])
  407.                             {
  408.                                 switch(Opts[i] -> xemo_type)
  409.                                 {
  410.                                     case XEMO_BOOLEAN:
  411.  
  412.                                         LT_New(Handle,
  413.                                             LA_Type,    CHECKBOX_KIND,
  414.                                             LA_LabelText,    Opts[i] -> xemo_description,
  415.                                             LA_ID,        GAD_SPECIAL + i,
  416.                                             GTCB_Checked,    GetOptionMode(Opts[i]),
  417.                                         TAG_DONE);
  418.  
  419.                                         break;
  420.  
  421.                                     case XEMO_LONG:
  422.  
  423.                                         LT_New(Handle,
  424.                                             LA_Type,        INTEGER_KIND,
  425.                                             LA_LabelText,        Opts[i] -> xemo_description,
  426.                                             LA_ID,            GAD_SPECIAL + i,
  427.                                             LA_Chars,        15,
  428.                                             GTIN_Number,        Atol(Opts[i] -> xemo_value),
  429.                                             LAIN_UseIncrementers,    TRUE,
  430.                                         TAG_DONE);
  431.  
  432.                                         break;
  433.  
  434.                                     case XEMO_STRING:
  435.  
  436.                                         LT_New(Handle,
  437.                                             LA_Type,    STRING_KIND,
  438.                                             LA_LabelText,    Opts[i] -> xemo_description,
  439.                                             LA_ID,        GAD_SPECIAL + i,
  440.                                             LA_Chars,    15,
  441.                                             GTST_String,    Opts[i] -> xemo_value,
  442.                                             GTST_MaxChars,    Opts[i] -> xemo_length - 1,
  443.                                         TAG_DONE);
  444.  
  445.                                         break;
  446.  
  447.                                     case XEMO_COMMPAR:
  448.  
  449.                                         LT_New(Handle,
  450.                                             LA_Type,    STRING_KIND,
  451.                                             LA_LabelText,    Opts[i] -> xemo_description,
  452.                                             LA_ID,        GAD_SPECIAL + i,
  453.                                             LA_Chars,    15,
  454.                                             LA_HighLabel,    TRUE,
  455.                                             GTST_String,    Opts[i] -> xemo_value,
  456.                                             GTST_MaxChars,    Opts[i] -> xemo_length - 1,
  457.                                         TAG_DONE);
  458.  
  459.                                         break;
  460.  
  461.                                     case XEMO_HEADER:
  462.  
  463.                                         LT_New(Handle,
  464.                                             LA_Type,    TEXT_KIND,
  465.                                             LA_LabelText,    Opts[i] -> xemo_description,
  466.                                             LA_HighLabel,    TRUE,
  467.                                             GTTX_Text,    " ",
  468.                                         TAG_DONE);
  469.  
  470.                                         break;
  471.  
  472.                                     case XEMO_COMMAND:
  473.  
  474.                                         LT_New(Handle,
  475.                                             LA_Type,    BUTTON_KIND,
  476.                                             LA_LabelText,    Opts[i] -> xemo_description,
  477.                                             LA_ID,        GAD_SPECIAL + i,
  478.                                             LA_Chars,    15,
  479.                                         TAG_DONE);
  480.  
  481.                                         break;
  482.                                 }
  483.                             }
  484.  
  485.                             if(i == Split)
  486.                             {
  487.                                 LT_EndGroup(Handle);
  488.  
  489.                                 LT_New(Handle,
  490.                                     LA_Type,    VERTICAL_KIND,
  491.                                 TAG_DONE);
  492.                             }
  493.                         }
  494.  
  495.                         LT_EndGroup(Handle);
  496.                     }
  497.  
  498.                     LT_EndGroup(Handle);
  499.                 }
  500.  
  501.                 LT_New(Handle,
  502.                     LA_Type,    VERTICAL_KIND,
  503.                 TAG_DONE);
  504.                 {
  505.                     LT_New(Handle,LA_Type,XBAR_KIND,LAXB_FullSize,TRUE,TAG_DONE);
  506.  
  507.                     LT_EndGroup(Handle);
  508.                 }
  509.  
  510.                 LT_New(Handle,LA_Type,HORIZONTAL_KIND,
  511.                     LAGR_SameSize,    TRUE,
  512.                     LAGR_Spread,    TRUE,
  513.                 TAG_DONE);
  514.                 {
  515.                     LT_New(Handle,
  516.                         LA_Type,    BUTTON_KIND,
  517.                         LA_LabelID,    MSG_GLOBAL_USE_GAD,
  518.                         LA_ID,        GAD_USE,
  519.                         LABT_ReturnKey,    TRUE,
  520.                         LABT_ExtraFat,    TRUE,
  521.                     TAG_DONE);
  522.  
  523.                     LT_New(Handle,
  524.                         LA_Type,    BUTTON_KIND,
  525.                         LA_LabelID,    MSG_GLOBAL_CANCEL_GAD,
  526.                         LA_ID,        GAD_CANCEL,
  527.                         LABT_EscKey,    TRUE,
  528.                         LABT_ExtraFat,    TRUE,
  529.                     TAG_DONE);
  530.  
  531.                     LT_EndGroup(Handle);
  532.                 }
  533.  
  534.                 LT_EndGroup(Handle);
  535.             }
  536.  
  537.             if(PanelWindow = LT_Build(Handle,
  538.                 LAWN_TitleText,        OptionTitle ? OptionTitle : LocaleString(MSG_V36_1840),
  539.                 LAWN_IDCMP,        IDCMP_CLOSEWINDOW,
  540.                 LAWN_HelpHook,        &GuideHook,
  541.                 LAWN_Parent,        Window,
  542.                 WA_DepthGadget,        TRUE,
  543.                 WA_CloseGadget,        TRUE,
  544.                 WA_DragBar,        TRUE,
  545.                 WA_RMBTrap,        TRUE,
  546.                 WA_Activate,        TRUE,
  547.                 WA_SimpleRefresh,    TRUE,
  548.             TAG_DONE))
  549.             {
  550.                 struct IntuiMessage    *Message;
  551.                 BOOLEAN             Done = FALSE;
  552.                 ULONG             MsgClass;
  553.                 UWORD             MsgCode;
  554.                 struct Gadget        *MsgGadget;
  555.                 BOOLEAN             CheckFlags = FALSE;
  556.  
  557.                 PushWindow(PanelWindow);
  558.  
  559.                 LT_ShowWindow(Handle,TRUE);
  560.  
  561.                 do
  562.                 {
  563.                     if(Wait(PORTMASK(PanelWindow -> UserPort) | SIG_BREAK) & SIG_BREAK)
  564.                         break;
  565.  
  566.                     while(Message = (struct IntuiMessage *)LT_GetIMsg(Handle))
  567.                     {
  568.                         MsgClass    = Message -> Class;
  569.                         MsgCode        = Message -> Code;
  570.                         MsgGadget    = (struct Gadget *)Message -> IAddress;
  571.  
  572.                         LT_ReplyIMsg(Message);
  573.  
  574.                         if(MsgClass == IDCMP_CLOSEWINDOW)
  575.                             Done = TRUE;
  576.  
  577.                         if(MsgClass == IDCMP_GADGETUP)
  578.                         {
  579.                             switch(MsgGadget -> GadgetID)
  580.                             {
  581.                                 case GAD_USE:
  582.  
  583.                                     LT_UpdateStrings(Handle);
  584.  
  585.                                     Done = CheckFlags = TRUE;
  586.                                     break;
  587.  
  588.                                 case GAD_CANCEL:
  589.  
  590.                                     Done = TRUE;
  591.                                     break;
  592.  
  593.                                 default:
  594.  
  595.                                     if(MsgGadget -> GadgetID - GAD_SPECIAL < NumOpts)
  596.                                     {
  597.                                         i = MsgGadget -> GadgetID - GAD_SPECIAL;
  598.  
  599.                                         if(Opts[i] -> xemo_type == XEMO_COMMAND || (Opts[i] -> xemo_type == XEMO_COMMPAR && MsgCode != '\t'))
  600.                                         {
  601.                                             Flags = (1L << i);
  602.  
  603.                                             Done = CheckFlags = TRUE;
  604.                                         }
  605.                                     }
  606.  
  607.                                     break;
  608.                             }
  609.                         }
  610.                     }
  611.                 }
  612.                 while(!Done);
  613.  
  614.                 PopWindow();
  615.  
  616.                 if(CheckFlags)
  617.                 {
  618.                     STRPTR String;
  619.  
  620.                     LT_LockWindow(PanelWindow);
  621.  
  622.                     for(i = 0 ; i < NumOpts ; i++)
  623.                     {
  624.                         if(Opts[i])
  625.                         {
  626.                             switch(Opts[i] -> xemo_type)
  627.                             {
  628.                                 case XEMO_BOOLEAN:
  629.  
  630.                                     if(LT_GetAttributes(Handle,GAD_SPECIAL + i,TAG_DONE) != GetOptionMode(Opts[i]))
  631.                                     {
  632.                                         Flags |= (1L << i);
  633.  
  634.                                         if(LT_GetAttributes(Handle,GAD_SPECIAL + i,TAG_DONE))
  635.                                             strcpy(Opts[i] -> xemo_value,"yes");
  636.                                         else
  637.                                             strcpy(Opts[i] -> xemo_value,"no");
  638.  
  639.                                         NewOptions = TRUE;
  640.                                     }
  641.  
  642.                                     break;
  643.  
  644.                                 case XEMO_LONG:
  645.  
  646.                                     if(Atol(Opts[i] -> xemo_value) != LT_GetAttributes(Handle,GAD_SPECIAL + i,TAG_DONE))
  647.                                     {
  648.                                         Flags |= (1L << i);
  649.  
  650.                                         SPrintf(Opts[i] -> xemo_value,"%ld",LT_GetAttributes(Handle,GAD_SPECIAL + i,TAG_DONE));
  651.  
  652.                                         NewOptions = TRUE;
  653.                                     }
  654.  
  655.                                     break;
  656.  
  657.                                 case XEMO_COMMPAR:
  658.                                 case XEMO_STRING:
  659.  
  660.                                     if(String = (STRPTR)LT_GetAttributes(Handle,GAD_SPECIAL + i,TAG_DONE))
  661.                                     {
  662.                                         if(strcmp(Opts[i] -> xemo_value,String))
  663.                                         {
  664.                                             Flags |= (1L << i);
  665.  
  666.                                             strcpy(Opts[i] -> xemo_value,(STRPTR)LT_GetAttributes(Handle,GAD_SPECIAL + i,TAG_DONE));
  667.  
  668.                                             NewOptions = TRUE;
  669.                                         }
  670.                                     }
  671.  
  672.                                     break;
  673.                             }
  674.                         }
  675.                     }
  676.  
  677.                     LT_UnlockWindow(PanelWindow);
  678.                 }
  679.                 else
  680.                     Flags = NULL;
  681.             }
  682.  
  683.             LT_DeleteHandle(Handle);
  684.         }
  685.  
  686.         return(Flags);
  687.     }
  688.     else
  689.         return(NULL);
  690. }
  691.  
  692.     /* xem_swrite():
  693.      *
  694.      *    Send a few bytes across the serial line.
  695.      */
  696.  
  697. STATIC LONG __saveds __asm
  698. xem_swrite(register __a0 STRPTR Buffer,register __d0 LONG Size)
  699. {
  700.     if(WriteRequest)
  701.     {
  702.         SerWrite(Buffer,Size);
  703.  
  704.         return(0);
  705.     }
  706.     else
  707.         return(-1);
  708. }
  709.  
  710.     /* xem_sbreak():
  711.      *
  712.      *    Send a break signal across the serial line.
  713.      */
  714.  
  715. STATIC LONG __asm __saveds
  716. xem_sbreak(VOID)
  717. {
  718.     if(!WriteRequest)
  719.         return(-1);
  720.     else
  721.     {
  722.         SendBreak();
  723.  
  724.         return(0);
  725.     }
  726. }
  727.  
  728.     /* xem_sstart():
  729.      *
  730.      *    Restart serial read activity.
  731.      */
  732.  
  733. STATIC VOID __asm __saveds
  734. xem_sstart(VOID)
  735. {
  736.     RestartSerial();
  737. }
  738.  
  739.     /* xem_sstop():
  740.      *
  741.      *    Stop serial read activity.
  742.      */
  743.  
  744. STATIC LONG __asm __saveds
  745. xem_sstop(VOID)
  746. {
  747.     StopSerialRead();
  748.  
  749.     return(0);
  750. }
  751.  
  752.     /* xem_tgets(STRPTR Prompt,STRPTR Buffer,ULONG Size):
  753.      *
  754.      *    Get a string from the user.
  755.      */
  756.  
  757. STATIC LONG __saveds __asm
  758. xem_tgets(register __a0 STRPTR Prompt,register __a1 STRPTR Buffer,register __d0 ULONG Size)
  759. {
  760.     enum    {    GAD_OK=1,GAD_CANCEL,GAD_STRING };
  761.  
  762.     struct LayoutHandle    *Handle;
  763.     LONG             Success = FALSE;
  764.     UBYTE             LocalBuffer[256];
  765.  
  766.     if(strlen(Buffer) > 255)
  767.     {
  768.         CopyMem(Buffer,LocalBuffer,255);
  769.  
  770.         LocalBuffer[255] = 0;
  771.     }
  772.     else
  773.         strcpy(LocalBuffer,Buffer);
  774.  
  775.     if(!Prompt)
  776.         Prompt = LocaleString(MSG_TERMXPR_INPUT_REQUIRED_TXT);
  777.  
  778.     if(Handle = LT_CreateHandleTags(Window -> WScreen,
  779.         LH_LocaleHook,    &LocaleHook,
  780.     TAG_DONE))
  781.     {
  782.         struct Window *PanelWindow;
  783.  
  784.         LT_New(Handle,
  785.             LA_Type,    VERTICAL_KIND,
  786.         TAG_DONE);
  787.         {
  788.             LT_New(Handle,
  789.                 LA_Type,    VERTICAL_KIND,
  790.                 LA_LabelText,    Prompt,
  791.             TAG_DONE);
  792.             {
  793.                 LT_New(Handle,
  794.                     LA_Type,    STRING_KIND,
  795.                     LA_STRPTR,    LocalBuffer,
  796.                     LA_Chars,    30,
  797.                     GTST_MaxChars,    Size,
  798.                 TAG_DONE);
  799.  
  800.                 LT_EndGroup(Handle);
  801.             }
  802.  
  803.             LT_New(Handle,
  804.                 LA_Type,VERTICAL_KIND,
  805.             TAG_DONE);
  806.             {
  807.                 LT_New(Handle,
  808.                     LA_Type,    XBAR_KIND,
  809.                     LAXB_FullSize,    TRUE,
  810.                 TAG_DONE);
  811.  
  812.                 LT_EndGroup(Handle);
  813.             }
  814.  
  815.             LT_New(Handle,LA_Type,HORIZONTAL_KIND,
  816.                 LAGR_SameSize,    TRUE,
  817.                 LAGR_Spread,    TRUE,
  818.             TAG_DONE);
  819.             {
  820.                 LT_New(Handle,
  821.                     LA_Type,    BUTTON_KIND,
  822.                     LA_LabelID,    MSG_TERMXPR_OKAY_GAD,
  823.                     LA_ID,        GAD_OK,
  824.                     LABT_ReturnKey,    TRUE,
  825.                     LABT_ExtraFat,    TRUE,
  826.                 TAG_DONE);
  827.  
  828.                 LT_New(Handle,
  829.                     LA_Type,    BUTTON_KIND,
  830.                     LA_LabelID,    MSG_GLOBAL_CANCEL_GAD,
  831.                     LA_ID,        GAD_CANCEL,
  832.                     LABT_EscKey,    TRUE,
  833.                     LABT_ExtraFat,    TRUE,
  834.                 TAG_DONE);
  835.  
  836.                 LT_EndGroup(Handle);
  837.             }
  838.         }
  839.  
  840.         if(PanelWindow = LT_Build(Handle,
  841.             LAWN_TitleID,        MSG_GLOBAL_ENTER_TEXT_TXT,
  842.             LAWN_IDCMP,        IDCMP_CLOSEWINDOW,
  843.             LAWN_HelpHook,        &GuideHook,
  844.             LAWN_Parent,        Window,
  845.             WA_DepthGadget,        TRUE,
  846.             WA_CloseGadget,        TRUE,
  847.             WA_DragBar,        TRUE,
  848.             WA_RMBTrap,        TRUE,
  849.             WA_Activate,        TRUE,
  850.             WA_SimpleRefresh,    TRUE,
  851.         TAG_DONE))
  852.         {
  853.             struct IntuiMessage    *Message;
  854.             BOOLEAN             Done = FALSE;
  855.             ULONG             MsgClass;
  856.             UWORD             MsgCode;
  857.             struct Gadget        *MsgGadget;
  858.  
  859.             PushWindow(PanelWindow);
  860.  
  861.             LT_ShowWindow(Handle,TRUE);
  862.  
  863.             LT_Activate(Handle,GAD_STRING);
  864.  
  865.             do
  866.             {
  867.                 if(Wait(PORTMASK(PanelWindow -> UserPort) | SIG_BREAK) & SIG_BREAK)
  868.                     break;
  869.  
  870.                 while(Message = (struct IntuiMessage *)LT_GetIMsg(Handle))
  871.                 {
  872.                     MsgClass    = Message -> Class;
  873.                     MsgCode        = Message -> Code;
  874.                     MsgGadget    = (struct Gadget *)Message -> IAddress;
  875.  
  876.                     LT_ReplyIMsg(Message);
  877.  
  878.                     if(MsgClass == IDCMP_CLOSEWINDOW)
  879.                         Done = TRUE;
  880.  
  881.                     if(MsgClass == IDCMP_GADGETUP)
  882.                     {
  883.                         switch(MsgGadget -> GadgetID)
  884.                         {
  885.                             case GAD_STRING:
  886.  
  887.                                 if(MsgCode == '\r')
  888.                                 {
  889.                                     strcpy(Buffer,LocalBuffer);
  890.  
  891.                                     Success = Done = TRUE;
  892.  
  893.                                     LT_PressButton(Handle,GAD_OK);
  894.                                 }
  895.  
  896.                                 break;
  897.  
  898.                             case GAD_OK:
  899.  
  900.                                 strcpy(Buffer,LocalBuffer);
  901.  
  902.                                 Success = Done = TRUE;
  903.                                 break;
  904.  
  905.                             case GAD_CANCEL:
  906.  
  907.                                 Done = TRUE;
  908.                                 break;
  909.                         }
  910.                     }
  911.                 }
  912.             }
  913.             while(!Done);
  914.  
  915.             PopWindow();
  916.         }
  917.  
  918.         LT_DeleteHandle(Handle);
  919.     }
  920.  
  921.     return(Success);
  922. }
  923.  
  924.     /* xem_tbeep(ULONG Times,ULONG Delay):
  925.      *
  926.      *    Beep the terminal display.
  927.      */
  928.  
  929. STATIC VOID __saveds __asm
  930. xem_tbeep(register __d0 ULONG Times,register __d1 ULONG Delay)
  931. {
  932.     WORD i;
  933.  
  934.     for(i = 0 ; i < Times ; i++)
  935.     {
  936.             /* Handle the visual part. */
  937.  
  938.         if(Config -> TerminalConfig -> BellMode != BELL_AUDIBLE)
  939.         {
  940.             if(StatusProcess)
  941.                 Signal(StatusProcess,SIG_BELL);
  942.         }
  943.  
  944.             /* Let it beep. */
  945.  
  946.         if(Config -> TerminalConfig -> BellMode == BELL_AUDIBLE || Config -> TerminalConfig -> BellMode == BELL_BOTH)
  947.             SoundPlay(SOUND_BELL);
  948.     }
  949. }
  950.  
  951.     /* xem_macrodispatch(struct XEmulatorMacroKey *XEM_MacroKey):
  952.      *
  953.      *    Dispatch a macro key call.
  954.      */
  955.  
  956. STATIC LONG __saveds __asm
  957. xem_macrodispatch(register __a0 struct XEmulatorMacroKey *XEM_MacroKey)
  958. {
  959.     VOID (*Routine)(VOID);
  960.  
  961.         /* If a routine to call is available (most likely xON or xOFF),
  962.          * make a call to it, else process the macro key data.
  963.          */
  964.  
  965.     if(Routine = (VPTR)XEM_MacroKey -> xmk_UserData)
  966.         (*Routine)();
  967.     else
  968.         SerialCommand(MacroKeys -> Keys[XEM_MacroKey -> xmk_Qualifier][XEM_MacroKey -> xmk_Code - 0x50]);
  969.  
  970.     return(0);
  971. }
  972.  
  973.     /* SetEmulatorOptions(BYTE Mode):
  974.      *
  975.      *    Save or load the emulator options.
  976.      */
  977.  
  978. BYTE __regargs
  979. SetEmulatorOptions(BYTE Mode)
  980. {
  981.     BYTE Success = FALSE;
  982.  
  983.         /* Is the library available and running? */
  984.  
  985.     if(XEmulatorBase && XEM_IO)
  986.     {
  987.             /* Are we using the new library code? */
  988.  
  989.         if(XEmulatorBase -> lib_Version >= 4)
  990.         {
  991.                 /* Get the name of the library. */
  992.  
  993.             strcpy(SharedBuffer,FilePart(XEmulatorBase -> lib_Node . ln_Name));
  994.  
  995.                 /* Does it have any name? */
  996.  
  997.             if(SharedBuffer[0])
  998.             {
  999.                 UBYTE    OtherBuffer[50];
  1000.                 WORD    i;
  1001.  
  1002.                     /* Strip the `.library' bit. */
  1003.  
  1004.                 for(i = strlen(SharedBuffer) - 1 ; i >= 0 ; i--)
  1005.                 {
  1006.                     if(SharedBuffer[i] == '.')
  1007.                     {
  1008.                         SharedBuffer[i] = 0;
  1009.  
  1010.                         break;
  1011.                     }
  1012.                 }
  1013.  
  1014.                     /* What are we to do? */
  1015.  
  1016.                 if(Mode == XEM_PREFS_LOAD)
  1017.                 {
  1018.                         /* Restore settings... */
  1019.  
  1020.                     strcpy(OtherBuffer,"ENV:");
  1021.  
  1022.                     if(AddPart(OtherBuffer,SharedBuffer,50))
  1023.                     {
  1024.                             /* If we can't load them,
  1025.                              * reset to defaults.
  1026.                              */
  1027.  
  1028.                         if(!XEmulatorPreferences(XEM_IO,OtherBuffer,Mode))
  1029.                         {
  1030.                             strcpy(OtherBuffer,"ENV:xem");
  1031.  
  1032.                             if(AddPart(OtherBuffer,SharedBuffer,50))
  1033.                             {
  1034.                                 if(XEmulatorPreferences(XEM_IO,OtherBuffer,Mode))
  1035.                                     Success = TRUE;
  1036.                             }
  1037.                         }
  1038.                         else
  1039.                             Success = TRUE;
  1040.  
  1041.                         if(!Success)
  1042.                             XEmulatorPreferences(XEM_IO,NULL,XEM_PREFS_RESET);
  1043.                     }
  1044.                 }
  1045.                 else
  1046.                 {
  1047.                         /* Save settings to ENV: */
  1048.  
  1049.                     strcpy(OtherBuffer,"ENV:");
  1050.  
  1051.                     if(AddPart(OtherBuffer,SharedBuffer,50))
  1052.                     {
  1053.                         if(XEmulatorPreferences(XEM_IO,OtherBuffer,Mode))
  1054.                             Success = TRUE;
  1055.                     }
  1056.  
  1057.                     if(Success)
  1058.                     {
  1059.                         Success = FALSE;
  1060.  
  1061.                             /* Save settings to ENVARC: */
  1062.  
  1063.                         strcpy(OtherBuffer,"ENVARC:");
  1064.  
  1065.                         if(AddPart(OtherBuffer,SharedBuffer,50))
  1066.                         {
  1067.                             if(XEmulatorPreferences(XEM_IO,OtherBuffer,Mode))
  1068.                                 Success = TRUE;
  1069.                         }
  1070.                     }
  1071.                 }
  1072.             }
  1073.         }
  1074.     }
  1075.  
  1076.         /* Return result. */
  1077.  
  1078.     return(Success);
  1079. }
  1080.  
  1081.     /* SetupEmulator(BYTE OpenConsole):
  1082.      *
  1083.      *    Initialize the XEM_IO structure.
  1084.      */
  1085.  
  1086. STATIC BYTE
  1087. SetupEmulator(VOID)
  1088. {
  1089.     if(!XEM_IO)
  1090.     {
  1091.         if(XEM_IO = (struct XEM_IO *)AllocVec(sizeof(struct XEM_IO),MEMF_ANY | MEMF_CLEAR | MEMF_PUBLIC))
  1092.         {
  1093.             XEM_IO -> xem_window        = Window;
  1094.             XEM_IO -> xem_font        = CurrentFont;
  1095.             XEM_IO -> xem_signal        = &XEM_Signal;
  1096.             XEM_IO -> xem_screendepth    = GetBitMapDepth(Window -> WScreen -> RastPort . BitMap);
  1097.  
  1098.             XEM_IO -> xem_sread        = xem_sread;
  1099.             XEM_IO -> xem_swrite        = xem_swrite;
  1100.             XEM_IO -> xem_sflush        = xem_sflush;
  1101.             XEM_IO -> xem_sbreak        = xem_sbreak;
  1102.             XEM_IO -> xem_squery        = xem_squery;
  1103.             XEM_IO -> xem_sstart        = xem_sstart;
  1104.             XEM_IO -> xem_sstop        = xem_sstop;
  1105.  
  1106.             XEM_IO -> xem_tbeep        = xem_tbeep;
  1107.             XEM_IO -> xem_tgets        = xem_tgets;
  1108.             XEM_IO -> xem_toptions        = xem_toptions;
  1109.  
  1110.             XEM_IO -> xem_process_macrokeys    = xem_macrodispatch;
  1111.  
  1112.             return(TRUE);
  1113.         }
  1114.     }
  1115.     else
  1116.         return(FALSE);
  1117.  
  1118.     return(FALSE);
  1119. }
  1120.  
  1121.     /* CloseEmulator():
  1122.      *
  1123.      *    Close the emulation library.
  1124.      */
  1125.  
  1126. VOID
  1127. CloseEmulator()
  1128. {
  1129.     if(XEmulatorBase)
  1130.     {
  1131.         if(XEM_IO)
  1132.         {
  1133.             XEmulatorMacroKeyFilter(XEM_IO,NULL);
  1134.             XEmulatorCloseConsole(XEM_IO);
  1135.             XEmulatorCleanup(XEM_IO);
  1136.         }
  1137.  
  1138.         CloseLibrary(XEmulatorBase);
  1139.  
  1140.         if(XEM_IO)
  1141.             FreeVec(XEM_IO);
  1142.  
  1143.         strcpy(EmulationName,LocaleString(MSG_TERMXEM_NO_EMULATION_TXT));
  1144.  
  1145.         XEmulatorBase    = NULL;
  1146.         XEM_Signal    = NULL;
  1147.         XEM_IO        = NULL;
  1148.  
  1149.         RasterEnabled = TRUE;
  1150.  
  1151.         ClearCursor();
  1152.  
  1153.         Reset();
  1154.  
  1155.         DrawCursor();
  1156.     }
  1157. }
  1158.  
  1159.     /* OpenEmulator(STRPTR Name):
  1160.      *
  1161.      *    Open an emulation library.
  1162.      */
  1163.  
  1164. BYTE __regargs
  1165. OpenEmulator(STRPTR Name)
  1166. {
  1167.     CloseEmulator();
  1168.  
  1169.     XEM_HostData . Source        = NULL;
  1170.     XEM_HostData . Destination    = NULL;
  1171.     XEM_HostData . InESC        = FALSE;
  1172.     XEM_HostData . InCSI        = FALSE;
  1173.  
  1174.     if(XEmulatorBase = OpenLibrary(Name,0))
  1175.     {
  1176.         ClearCursor();
  1177.  
  1178.         Reset();
  1179.  
  1180.         if(SetupEmulator())
  1181.         {
  1182.             SetMask(RPort,DepthMask);
  1183.  
  1184.             ClearSerial();
  1185.  
  1186.             if(XEmulatorSetup(XEM_IO))
  1187.             {
  1188.                 RestartSerial();
  1189.  
  1190.                 SetEmulatorOptions(XEM_PREFS_LOAD);
  1191.  
  1192.                 if(XEmulatorOpenConsole(XEM_IO))
  1193.                 {
  1194.                     STRPTR LibName = FilePart(XEmulatorBase -> lib_Node . ln_Name);
  1195.  
  1196.                     strcpy(EmulationName,&LibName[3]);
  1197.  
  1198.                     EmulationName[strlen(EmulationName) - 8] = 0;
  1199.  
  1200.                     SetupXEM_MacroKeys(MacroKeys);
  1201.  
  1202.                     return(TRUE);
  1203.                 }
  1204.             }
  1205.             else
  1206.                 RestartSerial();
  1207.         }
  1208.  
  1209.         DrawCursor();
  1210.  
  1211.         CloseLibrary(XEmulatorBase);
  1212.     }
  1213.  
  1214.     strcpy(EmulationName,LocaleString(MSG_TERMXEM_NO_EMULATION_TXT));
  1215.  
  1216.     XEmulatorBase    = NULL;
  1217.     XEM_Signal    = NULL;
  1218.  
  1219.     return(FALSE);
  1220. }
  1221.  
  1222.     /* xOn():
  1223.      *
  1224.      *    Small local routine, complements XOff().
  1225.      */
  1226.  
  1227. STATIC VOID
  1228. xOn(VOID)
  1229. {
  1230.     UBYTE c = XON;
  1231.  
  1232.     if(Config -> SerialConfig -> xONxOFF)
  1233.         Status = STATUS_HOLDING;
  1234.  
  1235.     if(Config -> SerialConfig -> PassThrough)
  1236.         SerWrite(&c,1);
  1237. }
  1238.  
  1239.     /* xOff():
  1240.      *
  1241.      *    Small local routine, complements XOn() in Serial.c
  1242.      */
  1243.  
  1244. STATIC VOID
  1245. xOff(VOID)
  1246. {
  1247.     UBYTE c = XOF;
  1248.  
  1249.     if(Status == STATUS_HOLDING)
  1250.         Status = STATUS_READY;
  1251.  
  1252.     if(Config -> SerialConfig -> PassThrough)
  1253.         SerWrite(&c,1);
  1254. }
  1255.  
  1256.     /* SetupXEM_MacroKeys(struct MacroKeys *Keys):
  1257.      *
  1258.      *    Sets up the internal representation of the macro key
  1259.      *    data to fit the XEM specification.
  1260.      */
  1261.  
  1262. VOID __regargs
  1263. SetupXEM_MacroKeys(struct MacroKeys *Keys)
  1264. {
  1265.         /* Are we allowed to do what we want to do? */
  1266.  
  1267.     if(XEM_MacroKeys && XEmulatorBase && Config -> TerminalConfig -> EmulationMode == EMULATION_EXTERNAL)
  1268.     {
  1269.         WORD i,j,k = 0;
  1270.  
  1271.             /* Clear the macro list. */
  1272.  
  1273.         NewList(&XEM_MacroList);
  1274.  
  1275.             /* Run down the list of qualifiers. */
  1276.  
  1277.         for(i = XMKQ_NONE ; i <= XMKQ_CONTROL ; i++)
  1278.         {
  1279.                 /* Run down the function keys. */
  1280.  
  1281.             for(j = 0 ; j < 10 ; j++)
  1282.             {
  1283.                     /* If the key has no data attached,
  1284.                      * don't use it in the list.
  1285.                      */
  1286.  
  1287.                 if(Keys -> Keys[i][j][0])
  1288.                 {
  1289.                     XEM_MacroKeys[k] . xmk_Type        = XMKT_RAWKEY;
  1290.                     XEM_MacroKeys[k] . xmk_Qualifier    = i;
  1291.                     XEM_MacroKeys[k] . xmk_Code        = 0x50 + j;
  1292.                     XEM_MacroKeys[k] . xmk_UserData        = NULL;
  1293.  
  1294.                     AddTail(&XEM_MacroList,(struct Node *)&XEM_MacroKeys[k++]);
  1295.                 }
  1296.             }
  1297.         }
  1298.  
  1299.             /* Take care of the rest, add support for the xON key. */
  1300.  
  1301.         XEM_MacroKeys[k] . xmk_Type         = XMKT_COOKED;
  1302.         XEM_MacroKeys[k] . xmk_Qualifier    = NULL;
  1303.         XEM_MacroKeys[k] . xmk_Code        = XON;
  1304.         XEM_MacroKeys[k] . xmk_UserData        = (APTR)xOn;
  1305.  
  1306.         AddTail(&XEM_MacroList,(struct Node *)&XEM_MacroKeys[k++]);
  1307.  
  1308.             /* Take care of the xOFF key. */
  1309.  
  1310.         XEM_MacroKeys[k] . xmk_Type         = XMKT_COOKED;
  1311.         XEM_MacroKeys[k] . xmk_Qualifier    = NULL;
  1312.         XEM_MacroKeys[k] . xmk_Code        = XOF;
  1313.         XEM_MacroKeys[k] . xmk_UserData        = (APTR)xOff;
  1314.  
  1315.         AddTail(&XEM_MacroList,(struct Node *)&XEM_MacroKeys[k]);
  1316.  
  1317.             /* Make the emulator notice the new settings. */
  1318.  
  1319.         XEmulatorMacroKeyFilter(XEM_IO,&XEM_MacroList);
  1320.     }
  1321. }
  1322.